const { log } = require("console");
let http = require("http");
// 引入url模块
let url = require("url");

//数组--保存--待办事项
let todoList = [{ id: 1, title: "aaa", type: 1 }];

let server = http.createServer(function (request, response) {
  //前端请求的url--网址
  console.log(request.url);
  // 解析url
  let urlObj = url.parse(request.url, true);
  console.log(111);
  console.log(urlObj);
  switch (urlObj.pathname) {
    case "/ap/api/todos/create.php":
      {
        console.log("创建待办事项接口");
        response.writeHead(200, {
          "content-type": "application/json; charset=utf-8",
          "Access-Control-Allow-Origin": "*",
        });
        //响应体 中写入内容
        let obj = { code: "1", msg: "创建成功", params: urlObj.query };
        //   把后端想要返回的数据转为json字符串--返回给前端
        // todoList.push({
        //   id: Date.now(),
        //   title: urlObj.query.title,
        //   type: urlObj.query.type,
        // }); //代办的数据存入 数组

        todoList.push({
          id: Date.now(),
          ...urlObj.query, //展开query 对象里面的属性 都放入 {}
        }); //代办的数据存入 数组

        console.log(todoList);
        response.write(JSON.stringify(obj));
        response.end(); //结束回应
      }
      break;

    case "/ap/api/todos/readlist.php":
      {
        response.writeHead(200, {
          "content-type": "application/json; charset=utf-8",
          "Access-Control-Allow-Origin": "*",
        });
        //响应体 中写入内容
        let obj = { code: "1", msg: "获取列表", data: todoList };
        //   把后端想要返回的数据转为json字符串--返回给前端
        response.write(JSON.stringify(obj));
        response.end(); //结束回应
      }
      break;

    case "/ap/api/todos/delete.php":
      {
        console.log("创建待办事项接口");
        response.writeHead(200, {
          "content-type": "application/json; charset=utf-8",
          "Access-Control-Allow-Origin": "*",
        });
        //响应体 中写入内容
        let obj = { code: "1", msg: "删除接口", params: urlObj.query };
        let id = urlObj.query.id;

        // 删除--
        todoList; //中 元素的id 和 变量 id相当的 元素
        //
        todoList = todoList.filter((v) => v.id != id);

        //   把后端想要返回的数据转为json字符串--返回给前端
        response.write(JSON.stringify(obj));
        response.end(); //结束回应
      }
      break;
    case "/ap/api/todos/update.php":
      {
        response.writeHead(200, {
          "content-type": "application/json; charset=utf-8",
          "Access-Control-Allow-Origin": "*",
        });
        //响应体 中写入内容
        let obj = { code: "1", msg: "修改成功", params: urlObj.query };
        //   把后端想要返回的数据转为json字符串--返回给前端
        //
        let id = urlObj.query.id;
        // 修改 todoList 数组中元素的id 变量id相等的 个元素
        let updateObj = todoList.find((v) => v.id == id);
        updateObj.title = urlObj.query.title;
        updateObj.type = urlObj.query.type;

        response.write(JSON.stringify(obj));
        response.end(); //结束回应
      }
      break;

    default:
      {
        response.writeHead(404, {
          "content-type": "application/json; charset=utf-8",
        });
        let obj = { code: "0", msg: "接口地址不存在 not found" };
        response.write(JSON.stringify(obj));
        response.end(); //结束回应
      }

      break;
  }
});
// 监听的端口号可以自己写
// hostname 主机名（服务器的地址-127.0.0.1）
server.listen(3000, "192.168.5.27", function () {
  console.log("开始监听 http://192.168.5.27:3000");
});
